package pkg_member

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/pkg_member"
	pkg_memberReq "github.com/flipped-aurora/gin-vue-admin/server/model/pkg_member/request"
)

type JoinactivateService struct {
}

// CreateJoinactivate 创建参加活动记录
// Author [piexlmax](https://github.com/piexlmax)
func (joinactivateService *JoinactivateService) CreateJoinactivate(joinactivate *pkg_member.Joinactivate) (err error) {
	err = global.GVA_DB.Create(joinactivate).Error
	return err
}

// DeleteJoinactivate 删除参加活动记录
// Author [piexlmax](https://github.com/piexlmax)
func (joinactivateService *JoinactivateService) DeleteJoinactivate(ID string) (err error) {
	err = global.GVA_DB.Delete(&pkg_member.Joinactivate{}, "id = ?", ID).Error
	return err
}

// DeleteJoinactivateByIds 批量删除参加活动记录
// Author [piexlmax](https://github.com/piexlmax)
func (joinactivateService *JoinactivateService) DeleteJoinactivateByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]pkg_member.Joinactivate{}, "id in ?", IDs).Error
	return err
}

// UpdateJoinactivate 更新参加活动记录
// Author [piexlmax](https://github.com/piexlmax)
func (joinactivateService *JoinactivateService) UpdateJoinactivate(joinactivate pkg_member.Joinactivate) (err error) {
	err = global.GVA_DB.Save(&joinactivate).Error
	return err
}

// GetJoinactivate 根据ID获取参加活动记录
// Author [piexlmax](https://github.com/piexlmax)
func (joinactivateService *JoinactivateService) GetJoinactivate(ID string) (joinactivate pkg_member.Joinactivate, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&joinactivate).Error
	return
}

// GetJoinactivateInfoList 分页获取参加活动记录
// Author [piexlmax](https://github.com/piexlmax)
func (joinactivateService *JoinactivateService) GetJoinactivateInfoList(info pkg_memberReq.JoinactivateSearch) (list []pkg_member.Joinactivate, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&pkg_member.Joinactivate{})
	var joinactivates []pkg_member.Joinactivate
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&joinactivates).Error
	return joinactivates, total, err
}
